package com.kim.smartring.location;

import android.content.Context;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.kim.smartring.utils.ConfigUtils;

public class LocationUtils {

	private static final String TAG = "LocationUtils";
	private static final LocationUtils INSTANCE = new LocationUtils();
	private LocationClient locationClient;

	private Context context;

	public static LocationUtils getInstance() {
		return INSTANCE;
	}

	private LocationUtils() {
	}

	public void init(Context context, BDLocationListener locationListener) {
		this.context = context;
		if (locationClient == null) {
			locationClient = new LocationClient(this.context);

			LocationClientOption option = new LocationClientOption();
			option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
			option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度，默认值gcj02
			option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
			option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
			option.setPriority(LocationClientOption.NetWorkFirst); // 设置定位优先级
			option.setScanSpan(100000); // 定位时间间隔 100s
			option.setProdName("SmartRing");
			locationClient.setLocOption(option);

		}
		if (locationListener != null) {
			locationClient.registerLocationListener(locationListener);
		}
	}

	public void startLocation() {
		if (locationClient.isStarted()) {
			locationClient.stop();
		}
		locationClient.start();
		if (locationClient != null && locationClient.isStarted())
			locationClient.requestLocation();
		else
			Log.d("LocSDK3", "locClient is null or not started");
	}

	public void stopLocation() {
		if (locationClient.isStarted()) {
			locationClient.stop();
		}
	}

	public void startPOI() {
		if (locationClient != null && locationClient.isStarted())
			locationClient.requestPoi();
	}

	public void doLocation(Context context,
			final OnLocationReceiver onLocationReceiver) {
		init(context, new BDLocationListener() {

			@Override
			public void onReceivePoi(BDLocation arg0) {

			}

			@Override
			public void onReceiveLocation(BDLocation location) {

				stopLocation();

				if (location == null) {
					onLocationReceiver.onReceiver(false);
					return;
				}

				boolean needQueryAddressInfo = true;
				StringBuffer sb = new StringBuffer(256);

				sb.append("time : ");
				sb.append(location.getTime());
				sb.append("\nerror code : ");
				sb.append(location.getLocType());
				sb.append("\nlatitude : ");
				sb.append(location.getLatitude());
				sb.append("\nlontitude : ");
				sb.append(location.getLongitude());
				sb.append("\nradius : ");
				sb.append(location.getRadius());
				if (location.getLocType() == BDLocation.TypeGpsLocation) {
					sb.append("\nspeed : ");
					sb.append(location.getSpeed());
					sb.append("\nsatellite : ");
					sb.append(location.getSatelliteNumber());
				} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
					sb.append("\naddr : ");
					sb.append(location.getAddrStr());
					needQueryAddressInfo = false;
					// 保存用户位置信息
					ConfigUtils.setUserAddressInfo(location.getAddrStr());
				}
				Log.d(TAG, sb.toString());
				Log.d(TAG, "privince:" + location.getProvince());
				Log.d(TAG, "city:" + location.getCity());
				if (needQueryAddressInfo) { // 根据经纬度信息你查询用户位置信息

				}
				onLocationReceiver.onReceiver(true);
			}
		});
		startLocation();
	}

	public interface OnLocationReceiver {
		void onReceiver(boolean result);
	}

}
